package com.lvtulife.common.component.constants;


import com.lvtulife.common.component.LabelValue;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 系统常量工具类
 *
 * @author valuegroup
 */
public final class ConstantUtil {
    private static Constant constant = new Constant();
    private Map<String, String> parentKeyLabelMap = new HashMap<>();
    private Map<String, String> childKeyLabelMap = new HashMap<>();
    private Map<String, Object[]> parentKeyValueMap = new HashMap<>();
    private Map<String, List<LabelValue>> parentKeyBeanMap = new HashMap<>();

    private static final ConstantUtil list = new ConstantUtil();

    /* 单例 */
    public static ConstantUtil getInstance() {
        return list;
    }

    private ConstantUtil() {
        initContantMap();
    }

    /* 初始化常量集合 */
    private void initContantMap() {
        for (Class<?> oneClass : constant.getClass().getClasses()) {
            ParentLabel p = oneClass.getAnnotation(ParentLabel.class);
            if (p != null) {
                for (Class<?> twoClass : oneClass.getClasses()) {
                    p = twoClass.getAnnotation(ParentLabel.class);
                    if (p != null) {

                        /* 装载常量说明  */
                        String constantKey = oneClass.getSimpleName() + "." + twoClass.getSimpleName();

                        /* 装载常量值  */
                        List<Object> valueList = new ArrayList<>();
                        List<LabelValue> labelList = new ArrayList<>();
                        for (Field constant : twoClass.getFields()) {
                            Label l = constant.getAnnotation(Label.class);
                            if (l != null) {
                                try {
                                    Object value = constant.get(null);

                                    valueList.add(value);
                                    labelList.add(new LabelValue(l.value(), value.toString()));

                                    childKeyLabelMap.put(constantKey + "." + constant.getName(), l.value());
                                } catch (IllegalAccessException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        Object[] valArr = valueList.toArray(new Object[valueList.size()]);
                        Arrays.sort(valArr);

                        parentKeyValueMap.put(constantKey, valArr);
                        parentKeyLabelMap.put(constantKey, p.value());
                        parentKeyBeanMap.put(constantKey, labelList);
                    }
                }
            }
        }
    }

    /* 验证数据是否在有效范围内 */
    public boolean verifyConstant(String key, Object val) {
        Object[] values = parentKeyValueMap.get(key);
        if (values != null && Arrays.binarySearch(values, val) > 0) {
            return true;
        }
        return false;
    }


    /* 获取常量集合 */
    public List<LabelValue> getConstants(String key) {
        return parentKeyBeanMap.get(key);
    }

    /* 获取常量中文标签 ps:key = Base.State;value 数据状态 */
    public String getConstantLabel(String key) {
        return parentKeyLabelMap.get(key);
    }

    /* 获取常量中文标签 ps:key = Base.State;value 数据状态 */
    public String getChildConstantLabel(String key) {
        return childKeyLabelMap.get(key);
    }
}
